<HTML>
<HEAD>
<TITLE>merge()</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Rogue Wave Standard Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="memory-h.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="messages.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Rogue Wave C++ Standard Library Reference Guide</B></DIV>
<H2>merge()</H2>
<P><B>Module:</B>&nbsp;&nbsp;Standard C++ Library&nbsp;&nbsp;&nbsp;<B>Library:</B>&nbsp;&nbsp;<A HREF="2-9.html">Algorithms</A></P>

<PRE><HR><B><I>Function</I></B><HR></PRE>

<UL>
<LI><A HREF="#sec1">Local Index</A></LI>
<LI><A HREF="#sec2">Summary</A></LI>
<LI><A HREF="#sec3">Synopsis</A></LI>
<LI><A HREF="#sec4">Description</A></LI>
<LI><A HREF="#sec5">Complexity</A></LI>
<LI><A HREF="#sec6">Example</A></LI>
<LI><A HREF="#sec7">See Also</A></LI>
<LI><A HREF="#sec8">Standards Conformance</A></LI>
</UL>
<A NAME="sec1"><H3>Local Index</H3></A>
No Entries
<A NAME="sec2"><H3>Summary</H3></A>
<P>Algorithm that merges two sorted sequences into a third disjoint sorted sequence</P>
<A NAME="sec3"><H3>Synopsis</H3></A>

<PRE>#include &lt;algorithm&gt;

namespace std {
  template &lt;class InputIterator1, class InputIterator2,
            class OutputIterator&gt;
  OutputIterator
  merge(InputIterator1 start1, InputIterator1 finish1,
        InputIterator2 start2, InputIterator2 finish2,
        OutputIterator result);

  template &lt;class InputIterator1, class InputIterator2,
            class OutputIterator, class Compare&gt;
  OutputIterator
  merge(InputIterator1 start1, InputIterator1 finish1,
        InputIterator2 start2, InputIterator2 finish2,
        OutputIterator result, Compare comp);
}
</PRE>
<A NAME="sec4"><H3>Description</H3></A>
<P>The <SAMP>merge()</SAMP> algorithm merges two sorted sequences, specified by <SAMP>[start1, finish1)</SAMP> and <SAMP>[start2, finish2)</SAMP>, into the sequence specified by <SAMP>[result, result + (finish1 - start1) + (finish2 - start2))</SAMP>. The first version of the <SAMP>merge()</SAMP> algorithm uses <SAMP>operator&lt;()</SAMP> to compare elements in the two sequences. The second version uses the function object <SAMP>comp</SAMP>. If a comparison function is provided, <SAMP>merge()</SAMP> assumes that both sequences were sorted using the same ordering as the one given by the function object.</P>
<P>The merge is stable. This means that if the two original sequences contain equivalent elements, the elements from the first sequence always precede the matching elements from the second in the resulting sequence. The size of the result of a <SAMP>merge()</SAMP> is equal to the sum of the sizes of the two argument sequences. <SAMP>merge()</SAMP> returns an iterator that points to the end of the resulting sequence (in other words, <SAMP>result + (finish1 - start1) + (finish2 -start2)</SAMP>). The result of <SAMP>merge()</SAMP> is undefined if the resulting range overlaps with either of the original ranges. </P>
<P><SAMP>merge()</SAMP> assumes that all iterators in the range <SAMP>[result, result + (finish1 - start1) + (finish2 -</SAMP> <SAMP>start2)]</SAMP> are dereferenceable, unless <SAMP>result</SAMP> has been adapted by an insert iterator. </P>
<A NAME="sec5"><H3>Complexity</H3></A>
<P>At most <SAMP>(finish - start1) + (finish2 - start2) - 1</SAMP> comparisons are performed.</P>
<A NAME="sec6"><H3>Example</H3></A>

<UL><PRE>//
//  merge.cpp
//

#include &lt;algorithm&gt;    // advance, copy, inplace_merge, merge
#include &lt;functional&gt;   // less
#include &lt;iostream&gt;     // cout
#include &lt;iterator&gt;     // back_inserter, ostream_iterator
#include &lt;vector&gt;       // vector



int main ()
{
    typedef std::vector&lt;int, std::allocator&lt;int&gt; &gt; Vector;

    const Vector::value_type d1[] = { 1, 2, 3, 4};
    const Vector::value_type d2[] = { 11, 13, 15, 17, 
                                      12, 14, 16, 18};

    // Set up two vectors.
    Vector v1 (d1 + 0, d1 + sizeof d1 / sizeof *d1);
    Vector v2 (d1 + 0, d1 + sizeof d1 / sizeof *d1);

    // Set up four destination vectors.
    Vector v3 (d2 + 0, d2 + sizeof d2 / sizeof *d2);
    Vector v4 (v3);
    Vector v5 (v3);
    Vector v6 (v3);

    // Set up one empty vector.
    Vector v7;

    // Merge v1 with v2.
    std::merge (v1.begin (), v1.end (),
                v2.begin (), v2.end (), v3.begin ());

    // Now use comparator.
    std::merge (v1.begin (), v1.end (),
                v2.begin (), v2.end (), v4.begin (), 
                std::less&lt;int&gt;());

    // In place merge v5.
    Vector::iterator mid = v5.begin ();
    std::advance (mid, 4);   // equivalent to mid += 4 
                             // but more generic
    std::inplace_merge (v5.begin (), mid, v5.end ());

    // Now use a comparator on v6.
    mid = v6.begin ();
    std::advance (mid, 4);   // equivalent to mid += 4 
                             // but more generic
    std::inplace_merge (v6.begin (), mid, v6.end (), 
                        std::less&lt;int&gt;());    

    // Merge v1 and v2 to empty vector using insert iterator.
    std::merge (v1.begin (), v1.end (),
                v2.begin (), v2.end (), 
                std::back_inserter (v7));

    // Copy all to cout.
    std::ostream_iterator&lt;int, char, std::char_traits&lt;char&gt; &gt;
        out (std::cout," ");

    std::copy (v1.begin (), v1.end (), out);
    std::cout &lt;&lt; std::endl;
    std::copy (v2.begin(), v2.end (), out);
    std::cout &lt;&lt; std::endl;
    std::copy (v3.begin (), v3.end (), out);
    std::cout &lt;&lt; std::endl;
    std::copy (v4.begin (), v4.end (), out);
    std::cout &lt;&lt; std::endl;
    std::copy (v5.begin (), v5.end (), out);
    std::cout &lt;&lt; std::endl;
    std::copy (v6.begin (),v6.end (), out);
    std::cout &lt;&lt; std::endl;
    std::copy (v7.begin (), v7.end (), out);
    std::cout &lt;&lt; std::endl;

    // Merge v1 and v2 to cout.
    std::merge (v1.begin (), v1.end (), 
                v2.begin (), v2.end (), out);

    return 0;
}


Program Output:
</PRE></UL>
<UL><PRE>1 2 3 4 
1 2 3 4 
1 1 2 2 3 3 4 4 
1 1 2 2 3 3 4 4 
11 12 13 14 15 16 17 18 
11 12 13 14 15 16 17 18 
1 1 2 2 3 3 4 4 
1 1 2 2 3 3 4 4 
</PRE></UL>
<A NAME="sec7"><H3>See Also</H3></A>
<P><A HREF="containers.html">Containers</A>, <SAMP><A HREF="inplace-merge.html">inplace_merge()</A></SAMP></P>
<A NAME="sec8"><H3>Standards Conformance</H3></A>
<P><I>ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.3.4</I></P>

<BR>
<HR>
<A HREF="memory-h.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="messages.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A></BODY>
</HTML>
